from rest_framework.views import APIView
from users.models import User
from rest_framework.response import Response
from django.utils import timezone
import pytz
from django.conf import settings
from orders.models import OrderInfo
from datetime import timedelta
from rest_framework.viewsets import ViewSet
from rest_framework.decorators import action
from rest_framework.generics import ListAPIView
from goods.models import GoodsVisitCount
from meiduo_admin.serializers.home_serializer import *

# pytz.timezone('Asia/Shanghai')

# class UserTotalCount(APIView):
#
#     def get(self, request):
#         user_queryset = User.objects.all()
#         count = user_queryset.count()
#
#         utc_time = timezone.now()
#         local_time = utc_time.astimezone(tz=pytz.timezone(settings.TIME_ZONE))
#         return Response({
#             'count': count,
#             'date': local_time.date()
#         })
#
# class UserDayIncreView(APIView):
#     def get(self, request):
#         utc_time = timezone.now()
#         local_time = utc_time.astimezone(tz=pytz.timezone(settings.TIME_ZONE))
#         local_0_time = local_time.replace(hour=0, minute=0, second=0, microsecond=0)
#         count = User.objects.filter(date_joined__gte=local_0_time).count()
#         return Response({
#             'count': count,
#             'date': local_time.date()
#         })
#
# class UserActiveView(APIView):
#     def get(self, request):
#         local_0_time = timezone.now().astimezone(tz=pytz.timezone(settings.TIME_ZONE)).replace(hour=0, minute=0, second=0)
#         count = User.objects.filter(last_login__gte=local_0_time).count()
#         return ({
#             'count': count,
#             'date': local_0_time.date()
#         })
#
# class UserOrderView(APIView):
#     def get(self, request):
#         local_0_time = timezone.now().astimezone(tz=pytz.timezone(settings.TIME_ZONE)).replace(hour=0, minute=0,second=0)
#
#         # 方案一:从从表入手
#         # order_queryset = OrderInfo.objects.filter(create_time__gte=local_0_time)
#         # user_list = []
#         # for order in order_queryset:
#         #     user_list.append(order.user)
#         # count = len(set(user_list))
#         # return Response({
#         #     'count': count,
#         #     'date': local_0_time.date()
#         # })
#
#         # 方案二:从主表入手
#         user_queryset = User.objects.filter(orders__create_time__gte=local_0_time)
#         count = len(set(user_queryset))
#         return Response({
#             'count': count,
#             'date': local_0_time.date()
#         })
#
# class UserMonthIncreView(APIView):
#     def get(self, request):
#         cur_0_time = timezone.now().astimezone(tz=pytz.timezone(settings.TIME_ZONE)).replace(hour=0, minute=0, second=0)
#         start_0_time = cur_0_time - timedelta(days=29)
#         user_list = []
#         for index in range(30):
#             calc_0_time = start_0_time + timedelta(days=index)
#             next_0_time = start_0_time + timedelta(days=1)
#
#             count = User.objects.filter(date_joined__gte=calc_0_time, date_joined__lt=next_0_time).count()
#             user_list.append({
#                     'count': count,
#                     'date': calc_0_time.date()
#             })
#         return Response(user_list)

class HomeViewSet(ViewSet):
    @action(methods=['get'], detail=False)
    def total_count(self, request):
        user_queryset = User.objects.all()
        count = user_queryset.count()

        utc_time = timezone.now()
        local_time = utc_time.astimezone(tz=pytz.timezone(settings.TIME_ZONE))
        return Response({
            'count': count,
            'date': local_time.date()
        })

    @action(methods=['get'], detail=False)
    def day_increment(self, request):
        utc_time = timezone.now()
        local_time = utc_time.astimezone(tz=pytz.timezone(settings.TIME_ZONE))
        local_0_time = local_time.replace(hour=0, minute=0, second=0, microsecond=0)
        count = User.objects.filter(date_joined__gte=local_0_time).count()
        return Response({
            'count': count,
            'date': local_time.date()
        })

    @action(methods=['get'], detail=False)
    def day_active(self, request):
        local_0_time = timezone.now().astimezone(tz=pytz.timezone(settings.TIME_ZONE)).replace(hour=0, minute=0, second=0)
        count = User.objects.filter(last_login__gte=local_0_time).count()
        return Response({
            'count': count,
            'date': local_0_time.date()
        })

    @action(methods=['get'], detail=False)
    def day_orders(self, request):
        local_0_time = timezone.now().astimezone(tz=pytz.timezone(settings.TIME_ZONE)).replace(hour=0, minute=0, second=0)

        # 方案一:从从表入手
        # order_queryset = OrderInfo.objects.filter(create_time__gte=local_0_time)
        # user_list = []
        # for order in order_queryset:
        #     user_list.append(order.user)
        # count = len(set(user_list))
        # return Response({
        #     'count': count,
        #     'date': local_0_time.date()
        # })

        # 方案二:从主表入手
        user_queryset = User.objects.filter(orders__create_time__gte=local_0_time)
        count = len(set(user_queryset))
        return Response({
            'count': count,
            'date': local_0_time.date()
        })

    @action(methods=['get'], detail=False)
    def month_increment(self, request):
        cur_0_time = timezone.now().astimezone(tz=pytz.timezone(settings.TIME_ZONE)).replace(hour=0, minute=0, second=0)
        start_0_time = cur_0_time - timedelta(days=29)
        user_list = []
        for index in range(30):
            calc_0_time = start_0_time + timedelta(days=index)
            next_0_time = calc_0_time + timedelta(days=1)

            count = User.objects.filter(date_joined__gte=calc_0_time, date_joined__lt=next_0_time).count()
            user_list.append({
                'count': count,
                'date': calc_0_time.date()
            })
        return Response(user_list)

class GoodsVisitCountView(ListAPIView):
    queryset = GoodsVisitCount.objects.all()
    serializer_class = GoodsVisitCountSerializer

    def get_queryset(self):
        local_0_time = timezone.now().astimezone(tz=pytz.timezone(settings.TIME_ZONE)).replace(hour=0, minute=0, second=0)

        return self.queryset.filter(create_time__gte=local_0_time)